package cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.service.impl;

import cn.tt.tuantuanerp.tuantuanerp.comment.ex.ServiceException;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.SupplierStandardDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.Supplier;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.SupplierStandardListVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.SupplierStandardVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonPage;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.ServiceCode;
import cn.tt.tuantuanerp.tuantuanerp.purchase.service.ISupplierService;
import cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.mapper.SupplierMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    SupplierMapper mapper;

    /**
     * @author
     * 添加供应商信息
     */
    @Override
    public void add(SupplierStandardDTO supplier) {

        Supplier su=new Supplier ();
        BeanUtils.copyProperties(supplier,su);
        mapper.insertSupplier(su);
    }

    /**
     * @author zms
     * 根据id删除供应商信息
     */

    @Override
    public void deleteById(Integer id) {
        if(mapper.getById(id)==null){
            throw  new ServiceException ( ServiceCode.ERR_NOT_FOUND,"该供应商不存在");
        }
        mapper.deleteById(id);
    }

    /**
     * 根据id查询供应商信息
     * @param id
     * @return
     */

    @Override
    public SupplierStandardVO ById(Integer id) {
        SupplierStandardVO byId = mapper.getById ( id );
        if(byId==null){
            throw  new ServiceException ( ServiceCode.ERR_NOT_FOUND,"供应商不存在" );
        }
        return byId;
    }


    /**
     * @author zms
     * 查询供应商列表
     */

    @Override
    public JsonPage<SupplierStandardListVO> list(Integer startIndex, Integer pageSize) {
        PageHelper.startPage(startIndex,pageSize);
        List<SupplierStandardListVO> list = mapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }


    /**
     * 供应商列表（不分页）
     * @return
     */
    @Override
    public List<SupplierStandardListVO> listNoPage() {
        return  mapper.list();
    }
}
